#include "gameai.h"
#include "../GameEngine/engine.h"

QVector<CPUPlayer> GameAI::Players;

void GameAI::Tick()
{
    for(int i = 0; i < Players.count(); i++)
    {
        if(Players[i].Route.count() > 0 &&
           Players[i].MyMinion->GetCurrentTarget() == nullptr)
        {
            int point = Players[i].CurrentRoutePoint;

            Players[i].MyMinion->NewDestination(Players[i].Route[point].x(),
                                                Players[i].Route[point].y());

            if(equal(Players[i].MyMinion->GetX(), Players[i].Route[point].x(), 1.0) &&
               equal(Players[i].MyMinion->GetY(), Players[i].Route[point].y(), 1.0))
            {
                if(point + 1 < Players[i].Route.count())
                    Players[i].CurrentRoutePoint++;
                else
                    Players[i].Route.clear();
            }
        }
    }
}

void GameAI::AddAI(Minion *ControlledMinion, QVector<QPoint> &Route)
{
    CPUPlayer NewAI;
    NewAI.MyMinion = ControlledMinion;
    NewAI.Route = Route;
    NewAI.CurrentRoutePoint = 0;

    if(Route.count() > 0) NewAI.MyMinion->NewDestination(Route[0].x(), Route[0].y());

    Players.append(NewAI);
}

void GameAI::MinionRemoved(Minion *RemovedMinion)
{
    if(!RemovedMinion->IsMini()) return;

    for(int i = 0; i < Players.count(); i++)
    {
        if(Players[i].MyMinion == RemovedMinion)
        {
            Players.remove(i);
            return;
        }
    }
}
